package com.atguigu.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class dataGenSource {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(3);

        /**
         * 数据生成器的source有 4 个参数
         *      1：GeneratorFunction接口，需要实现，重写map方法，输入类型是固定的Long
         *      2：long类型，自动生成的数字序列（从0开始自增）的最大值，达到了这个值就停止
         *      3：限速策略，比如 每秒生成几条数据
         *      4：返回的类型
         */
        DataGeneratorSource<String> dataGeneratorSource = new DataGeneratorSource<>(
                new GeneratorFunction<Long, String>() {
                    @Override
                    public String map(Long aLong) throws Exception {
                        return "Numger" + aLong;
                    }
                }, 100, RateLimiterStrategy.perSecond(3),
                Types.STRING);

        env.fromSource(dataGeneratorSource, WatermarkStrategy.noWatermarks(), "dataGenSource").print();
        env.execute();
    }
}
